<?php

/**
 * spider_result_form actions.
 *
 * @package    sfproject
 * @subpackage spider_result_form
 * @author     Sergey Shadrin
 */
class spider_result_formActions extends sfActions
{

    public function executeIndex(sfWebRequest $request)
    {
        $this->spiderResults = SpiderResultPeer::doSelect(new Criteria());
    }

    public function executeShow(sfWebRequest $request)
    {
        $this->spiderResult = SpiderResultPeer::retrieveByPk($request->getParameter('spider_result_id'));
        $this->forward404Unless($this->spiderResult);
    }

    public function executeNew(sfWebRequest $request)
    {
        $this->form = new SpiderResultForm();
    }

    public function executeCreate(sfWebRequest $request)
    {
        $this->forward404Unless($request->isMethod(sfRequest::POST));
        $this->form = new SpiderResultForm();
        $this->processForm($request, $this->form);
        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request)
    {
        $spiderResult = $this->getSpiderResultById($request);
        $this->form = new SpiderResultForm($spiderResult);
    }

    public function executeUpdate(sfWebRequest $request)
    {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $spiderResult = $this->getSpiderResultById($request);
        $this->form = new SpiderResultForm($spiderResult);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request)
    {
        //$request->checkCSRFProtection();

        $spiderResult = $this->getSpiderResultById($request);

        $spiderResult->delete();

        $this->redirect('spider_result_form/index');
    }

    public function executeShowAjax(sfWebRequest $request)
    {
        $spiderResult = $this->getSpiderResultById($request);
        return $this->renderPartial('showResult', array('spiderResult' => $spiderResult));
    }

    public function executeDeleteAjax(sfWebRequest $request)
    {
        //$request->checkCSRFProtection();

        $spiderResult = $this->getSpiderResultById($request);

        $spiderResult->delete();

        return $this->renderText(1);
    }

    public function executeCreateAjax(sfWebRequest $request)
    {
        $this->forward404Unless($request->isMethod(sfRequest::POST));
        $form = new SpiderResultForm();
        return $this->processFormAjax($request, $form);
    }

    public function executeUpdateAjax(sfWebRequest $request)
    {
        $queryParams = $request->getParameter('spider_result');
        $spiderResult = SpiderResultPeer::retrieveByPk($queryParams['spider_result_id']);
        $this->forward404Unless($spiderResult);

        $form = new SpiderResultForm($spiderResult);
        return $this->processFormAjax($request, $form);
    }

    public function executeEditSpiderResultAjax(sfWebRequest $request)
    {
        $spiderResultId = $request->getParameter('request');
        if ( $spiderResultId !== null )
        {
            $spiderResult = SpiderResultPeer::retrieveByPk($spiderResultId);
            $this->forward404Unless($spiderResult);
            $form = new SpiderResultForm($spiderResult);
            $result = $this->renderPartial('form', array('form' => $form));
        }
        else
        {
            $result = $this->renderText("Error: incorrect request parameter");
        }
        return $result;
    }

    public function executeNewAjax(sfWebRequest $request)
    {
        $form = new SpiderResultForm();
        return $this->renderPartial('form', array('form' => $form));
    }

    public function executeRefreshTableAjax(sfWebRequest $request)
    {
        return $this->renderPartial('indexResult', array('spiderResults' => SpiderResultPeer::doSelect(new Criteria())));
    }

    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid())
        {
            $spiderResult = $form->save();

            $this->redirect('spider_result_form/edit?spider_result_id='.$spiderResult->getSpiderResultId());
        }
    }

    private function processFormAjax(sfWebRequest $request, sfForm $form)
    {
        $names = $request->getParameter($form->getName());
        $form->bind($names);
        $result = false;
        if ($form->isValid())
        {
            $form->save();
            $result = true;
        }
        else
        {
            $result = $form->getErrorSchema();
        }

        return $this->renderText($result);
    }

    private function getSpiderResultById(sfWebRequest $request)
    {
        $spiderResult = SpiderResultPeer::retrieveByPk($request->getParameter('spider_result_id'));
        $errorMessage = sprintf('Object SpiderResult does not exist (%s).', $request->getParameter('spider_result_id'));
        $this->forward404Unless($spiderResult, $errorMessage);
        return $spiderResult;
    }
}
